package org.cycuchy.hp.components.book.map {
	
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapType;
	import com.google.maps.controls.ControlPosition;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.controls.PositionControl;
	import com.google.maps.controls.ZoomControl;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	import org.cycuchy.hp.components.book.NumeredPage;

	public class MapPage_code extends NumeredPage {
		
		public var can:Canvas;
		private var ref:UIComponent;
		private var map:Map;		
		
		public function MapPage_code() {
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationComplete);
		}
		
		private function creationComplete(event:FlexEvent):void {
//			trace("UMapPage::creationComplete");
    		createInfoWidow();
			map = new Map();
			var key:String = null;
			
			//local
			//key = "ABQIAAAA5dsoWHqXFuFYjctBi_PKYBSJgpIJScf9Gb2GkIoXuAkP_3m9CBQW8EOvm4UbNu9YLeJX346oHMYatQ";
			
			//http://zyry.webpark.pl/f/
			key = "ABQIAAAA5dsoWHqXFuFYjctBi_PKYBTCuOSLLvWk-SsJUJ6noV6_SGqzYRSVibZgbQ7IN09BKS25jbecou3bvw";
			
			//http://zyry.appspot.com/
			//key = "ABQIAAAA5dsoWHqXFuFYjctBi_PKYBQQ8YpSESssZ-HBPMgXv8xID9svdxT9L6rhuihzkSqObi85W2seGr8H1g";
			
			map.key = key; 
			map.setSize(new Point(can.width, can.height));
			map.addEventListener(MapEvent.MAP_READY, onMapReady);

			ref = new UIComponent();
       		ref.addChild(map);
			can.addChild(ref);
		}
		
		public override function getBitmapData ():BitmapData {
			var bmd:BitmapData = new BitmapData(this.width, this.height, true, 0x00ffffff);
			ref.removeChild(map);
			bmd.draw(this);
			ref.addChild(map);
			return bmd;
		}
		
  		private function onMapReady(event:MapEvent):void {
  			map.removeEventListener(MapEvent.MAP_READY, onMapReady);
  			map.enableContinuousZoom();
  			map.setCenter(POLSKA_LL, 5, MapType.NORMAL_MAP_TYPE);
  			map.removeMapType(MapType.HYBRID_MAP_TYPE);
			var mapTypeControl:MapTypeControl = new MapTypeControl();
  			map.addControl(mapTypeControl);
  			map.addControl(new ZoomControl());
  			map.addControl(new PositionControl());
			mapTypeControl.setControlPosition(new ControlPosition(ControlPosition.ANCHOR_TOP_RIGHT, 26, 10));
  		}		
		
		private var infoWindowOptions:InfoWindowOptions;
		
		private var infoSprite:InfoWindowSprite;
		
		private static const POLSKA_LL:LatLng = new LatLng(52.148440, 19.304250);

		private static const DOM_GDANSK_LL:LatLng = new LatLng(54.418010, 18.574650);
		private static const DOM_GDANSK_UINT:uint = 0;
		private static const DOM_GDANSK_PARAM:Object = {
			title: "<font color='#434343' size='12' face='Verdana'><b>Tu mieszkamy</b></font>", 
			content: "<font color='#434343' size='11' face='Verdana'>Na osiedlu <b>Żabianka</b> przy ulicy <b>Pomorskiej 14</b>.</font>"};
		
		private static const DOM_KETRZYN_CYCEK1_LL:LatLng = new LatLng(54.094687, 21.379005);
		private static const DOM_KETRZYN_CYCEK1_UINT:uint = 1;
		private static const DOM_KETRZYN_CYCEK1_PARAM:Object = {
			title: "<font color='#434343' size='12' face='Verdana'><b>Tu mieszkał Cycuch</b></font>", 
			content: "<font color='#434343' size='11' face='Verdana'>Na osiedlu <b>Falklandy</b> przy ulicy <b>Królowej Bony 6</b>.</font>"};
		
		private static const DOM_KETRZYN_CYCEK2_LL:LatLng = new LatLng(54.077404, 21.374926);
		private static const DOM_KETRZYN_CYCEK2_UINT:uint = 2;
		private static const DOM_KETRZYN_CYCEK2_PARAM:Object = {
			title: "<font color='#434343' size='12' face='Verdana'><b>Tu mieszkał Cycek</b></font>", 
			content: "<font color='#434343' size='11' face='Verdana'>W centrum miasta przy ulicy <b>Sikorskiego 16.</b></font>"};
		
		private static const PRACA_CYCEK1_LL:LatLng = new LatLng(54.411975, 18.602300);
		private static const PRACA_CYCEK1_UINT:uint = 3;
		private static const PRACA_CYCEK1_PARAM:Object = {
			title: "<font color='#434343' size='12' face='Verdana'><b>Tu pracuje Cycek</b></font>", 
			content: "<font color='#434343' size='11' face='Verdana'>W firmie <b>Homeofarm</b> jako <b>z-ca kierownika działu kontroli jakości</b>.</font>"};
		
		private static const PRACA_CYCEK2_LL:LatLng = new LatLng(54.433086, 18.572125);
		private static const PRACA_CYCEK2_UINT:uint = 4;
		private static const PRACA_CYCEK2_PARAM:Object = {
			title: "<font color='#434343' size='12' face='Verdana'><b>Tu pracuje Cycuch</b></font>", 
			content: "<font color='#434343' size='11' face='Verdana'>W firmie <b>H and S</b> jako <b>projektant systemów informatycznych</b>.</font>"};		
		
		private function setCenter(i:uint, zoom:Number):void {
			var ll:LatLng;
			switch (i) {
				case DOM_GDANSK_UINT:
					ll = DOM_GDANSK_LL;
					break;
				case DOM_KETRZYN_CYCEK1_UINT:
					ll = DOM_KETRZYN_CYCEK1_LL;					
					break;
				case DOM_KETRZYN_CYCEK2_UINT:
					ll = DOM_KETRZYN_CYCEK2_LL;					
					break;
				case PRACA_CYCEK1_UINT:
					ll = PRACA_CYCEK1_LL;					
					break;
				case PRACA_CYCEK2_UINT:
					ll = PRACA_CYCEK2_LL;					
					break;
			}
			map.setCenter(ll, zoom);
		}
		
		public function domGdansk():void {
			setCenter(DOM_GDANSK_UINT, 17);
			setMapTypeMap();
			openInfoWindow(DOM_GDANSK_UINT);
		}

		public function domKetrzynCycek1():void {
			setCenter(DOM_KETRZYN_CYCEK1_UINT, 15);
			setMapTypeTerrain();
			openInfoWindow(DOM_KETRZYN_CYCEK1_UINT);
		}

		public function domKetrzynCycek2():void {
			setCenter(DOM_KETRZYN_CYCEK2_UINT, 15);
			setMapTypeTerrain();
			openInfoWindow(DOM_KETRZYN_CYCEK2_UINT);
		}
		
		public function pracaCycek1():void {
			setCenter(PRACA_CYCEK1_UINT, 15);
			setMapTypeMap();
			openInfoWindow(PRACA_CYCEK1_UINT);
		}
		
		public function pracaCycek2():void {
			setCenter(PRACA_CYCEK2_UINT, 15);
			setMapTypeMap();
			openInfoWindow(PRACA_CYCEK2_UINT);
		}
		
		private function setMapTypeMap():void {
			map.setMapType(MapType.NORMAL_MAP_TYPE);
		}

		private function setMapTypeSatelite():void {
			map.setMapType(MapType.SATELLITE_MAP_TYPE);
		}
			
		private function setMapTypeTerrain():void {
			map.setMapType(MapType.PHYSICAL_MAP_TYPE);
		}
		
		private function createInfoWidow():void {
			infoSprite = new InfoWindowSprite();
			infoWindowOptions = new InfoWindowOptions({
                customContent: infoSprite,
                customOffset: new Point(0, 10),
                customCloseRect: new Rectangle(50, -115, 20, 20)
              });
			/*
			infoWindowOptions = new InfoWindowOptions();
			
			var fillStyle:FillStyle = new FillStyle({color: 0xFFFFFF, alpha: 0.85});
			infoWindowOptions.fillStyle = fillStyle;
			
			var strokeStyle:StrokeStyle = new StrokeStyle({thickness: 1, alpha: 0.6, color: 0x434343});
			infoWindowOptions.strokeStyle = strokeStyle;
			
//			infoStyle.closeRGB = 0x434343;
//			infoStyle.closeAlpha = .6;
//			
			infoWindowOptions.cornerRadius = 3;
			
			var styleSheet:StyleSheet = new StyleSheet();
			styleSheet.setStyle("body", {color: "#434343", fontWeight: "bold", fontFamily: "Vardana"});
			infoWindowOptions.contentStyleSheet = styleSheet;
						
//			var style:TextStyle = new TextStyle();
//			style.autoSize = TextFieldAutoSize.CENTER;
// 			style.textFormat.bold = true;
// 			style.textFormat.color = 0x434343;
// 			style.textFormat.font = "Verdana";
//			infoStyle.titleStyle = style;
//			
//			infoStyle.padding = new Point(5, 5);	
//			
//			infoStyle.contentStyle.html = true;
			*/		
		}
		
		private function openInfoWindow(i:uint):void {
			var param:Object;
			switch (i) {
				case DOM_GDANSK_UINT:
					param = DOM_GDANSK_PARAM;
					break;
				case DOM_KETRZYN_CYCEK1_UINT:
					param = DOM_KETRZYN_CYCEK1_PARAM;					
					break;
				case DOM_KETRZYN_CYCEK2_UINT:
					param = DOM_KETRZYN_CYCEK2_PARAM;					
					break;
				case PRACA_CYCEK1_UINT:
					param = PRACA_CYCEK1_PARAM;					
					break;
				case PRACA_CYCEK2_UINT:
					param = PRACA_CYCEK2_PARAM;					
					break;
			}
			infoSprite.bodyText = param.content;
			infoSprite.titleText = param.title;
			map.openInfoWindow(map.getCenter(), infoWindowOptions);			
		} 
			
	}
	
}